package com.yb.project.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yb.common.domain.AjaxObject;
import com.yb.common.domain.PageResult;
import com.yb.common.utils.StringUtil;
import com.yb.project.db.service.ColumnService;
import com.yb.project.db.service.TableService;
import com.yb.project.system.service.SysConfigService;
import com.yb.project.system.service.SysContentService;
import com.yb.project.system.service.SysLogLoginService;
import com.yb.project.system.service.SysLogOperateService;
import com.yb.project.system.service.SysMenuService;
import com.yb.project.system.service.SysRoleMenuService;
import com.yb.project.system.service.SysRoleService;
import com.yb.project.system.service.SysUserRoleService;
import com.yb.project.system.service.SysUserService;

public class BaseController {

	protected HttpServletRequest request;
	protected HttpServletResponse response;

	/**
	 * BaseController 初始化
	 * @ModelAttribute，被注解的方法或属性会执行于 @RequestMapping 之前。
	 */
	@ModelAttribute
	protected void init(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}
	
	protected void startPage() {
		String pageNum = request.getParameter("page"); // 当前页数
		String pageSize = request.getParameter("rows"); // 每页记录数
		if (StringUtil.isNotEmpty(pageNum) && StringUtil.isNotEmpty(pageSize)) {
			PageHelper.startPage(Integer.valueOf(pageNum), Integer.valueOf(pageSize));
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected PageResult getPageInfo(List<?> list) {
		return new PageResult((int) new PageInfo(list).getTotal(), list);
	}
	
	protected AjaxObject toAjax(int rows) {
		return rows > 0 ? AjaxObject.newOk() : AjaxObject.newError();
	}
	
	protected AjaxObject toAjax(int rows, String msg) {
		return rows > 0 ? AjaxObject.newOk(msg + "，操作成功") : AjaxObject.newError(msg + "，操作失败");
	}

	@Autowired
	protected SysUserService sysUserService;
	@Autowired
	protected SysRoleService sysRoleService;
	@Autowired
	protected SysMenuService sysMenuService;
	@Autowired
	protected SysUserRoleService sysUserRoleService;
	@Autowired
	protected SysRoleMenuService sysRoleMenuService;
	@Autowired
	protected SysConfigService sysConfigService;
	@Autowired
	protected SysContentService sysContentService;
	@Autowired
	protected SysLogLoginService sysLogLoginService;
	@Autowired
	protected SysLogOperateService sysLogOperateService;

	@Autowired
	protected TableService tableService;
	@Autowired
	protected ColumnService columnService;
}
